// pages/my/my.js
import {
  ClassicModel
} from '../../models/Classic.js';

import {
  BookModel
} from '../../models/Book.js';

import {
  promise
} from '../../utils/common.js';

const classicModel = new ClassicModel();
const bookModel = new BookModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authorized:false,
    userInfo:null,
    classics: null,
    bookCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getUserInfoByPromise();
    this._getFavorCount();
    this._getMyFavor();
  },

  _getUserInfoByPromise(){
    promise(wx.getSetting)().then(data=>{
      if(data.authSetting["scope.userInfo"]){
        return promise(wx.getUserInfo)();
      }
    }).then(data=>{
      if(!data) return ;
      this.setData({
        userInfo:data.userInfo,
        authorized: true,
      });
    });
  },

  _getUserInfoByCallback(){
    wx.getSetting({
      success:data=>{
        if(data.authSetting["scope.userInfo"]){//表示已授权
          wx.getUserInfo({
            success:res=>{
              this.setData({
                userInfo:res.userInfo,
                authorized: true,
              });
            }
          });
          
        }
      }
    });
  },

  _getMyFavor(){
    classicModel.getMyFavor(res=>{
      this.setData({
        classics:res,
      });
    });
  },
  _getFavorCount(){
    bookModel.getMyBookCount().then(res=>{
      this.setData({
        bookCount:res.count,
      });
    });
  },

  onGetUserInfo: function(event){
    const userInfo = event.detail.userInfo;
    this.setData({
      userInfo:userInfo,
      authorized: true,
    });
  },
  onStudy:function(){
    wx.navigateTo({
      url: '/pages/course/course',
    })
  }
})